#include "pscheduler.h"
#include "ses_timer.h"
#include "ses_uart.h"



/* Indicates what task to execute. */
int static volatile number = 0;

void taskA(void) {
	sei();
	while (1) {
		printf("\n aaaaaaaaaaaa\n");
	}
}

void taskB(void) {
	sei();
	while (1) {
		printf("\n bbbbbbbbbbbb \n");
	}
}

void taskC(void) {
	sei();
	while (1) {
		printf("\n cccccccccccc \n");
	}
}

void initScheduler() {

	// initial task that starts the circle
	task = malloc(sizeof(ptask));

	list[0] = malloc(sizeof(ptask));
	list[1] = malloc(sizeof(ptask));
	list[2] = malloc(sizeof(ptask));
	list[3] = malloc(sizeof(ptask));

	// circle
	list[0]->next = list[1];
	list[1]->next = list[2];
	list[2]->next = list[3];
	list[3]->next = list[1];

	task = list[0];
	pstack = (void*) 1;
}

void run() {

	// started with A

	// continues with B
	if (number == 0) {
		number++;
		taskB();
	}

	// after that with C
	else if (number == 1) {
		number++;
		taskC();
	}

}

int main(void) {
	sei();				// enable interrupts
	led_allInit();		// init LEDs
	uart_init(57600);	// init UART
	stdout = uartout;	// enable terminal output
	initScheduler();	// init scheduler
	timer2_init(run);	// start timer with scheduler run
	taskA();			// start with task A
	return 0;
}
